package com.yuki.provider.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.yuki.api.common.CommonPage;
import com.yuki.api.model.OmsOrderReturnReason;
import com.yuki.api.model.OmsOrderReturnReasonExample;
import com.yuki.api.service.IOmsOrderReasonService;
import com.yuki.provider.mapper.OmsOrderReturnReasonMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @description:
 * @author: Yuki Tadayoshi
 * @time: 2021/3/26 12:45
 */
@Service(
        version = "1.0.0",
        interfaceName = "com.yuki.api.service.IOmsOrderReasonService",
        interfaceClass = IOmsOrderReasonService.class
)
@Transactional
public class OmsOrderReasonServiceImpl implements IOmsOrderReasonService {

    @Autowired
    private OmsOrderReturnReasonMapper returnReasonMapper;

    @Autowired
    private CacheManager cacheManager;

    private void clearOrderReturnReason() {
        cacheManager.getCache("OrderReturnReasonList").clear();
    }

    @CachePut(cacheNames = {"OrderReturnReason"}, key = "#returnReason.id")//Spring EL
    @Override
    public OmsOrderReturnReason create(OmsOrderReturnReason returnReason) {
        clearOrderReturnReason();//主动清空缓存 以便数据同步
        returnReason.setCreateTime(new Date());
        returnReasonMapper.insert(returnReason);
        return returnReason;
    }

    @CachePut(cacheNames = {"OrderReturnReason"},key ="#id")
    @Override
    public OmsOrderReturnReason update(Long id, OmsOrderReturnReason returnReason) {
        clearOrderReturnReason();
        returnReason.setId(id);
        returnReasonMapper.updateByPrimaryKey(returnReason);
        return returnReason;
    }

    @CacheEvict(cacheNames = "OrderReturnReason", key = "#ids[0]", allEntries = true, beforeInvocation = false)
    @Override
    public int delete(List<Long> ids) {
        clearOrderReturnReason();
        OmsOrderReturnReasonExample example = new OmsOrderReturnReasonExample();
        example.createCriteria().andIdIn(ids);
        return returnReasonMapper.deleteByExample(example);
    }

    @Cacheable(cacheNames = "OrderReturnReasonList", unless = "#result==null", key = "#pageNum+'-'+#pageSize")
    @Override
    public CommonPage list(Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        OmsOrderReturnReasonExample example = new OmsOrderReturnReasonExample();
        example.setOrderByClause("sort desc");
        return CommonPage.restPage(returnReasonMapper.selectByExample(example));
    }

    @Override
    public OmsOrderReturnReason updateStatus(Long id, Integer status) {
        clearOrderReturnReason();
        OmsOrderReturnReason record = getOmsOrderReturnReason(id);
        record.setStatus(status);
        OmsOrderReturnReasonExample example = new OmsOrderReturnReasonExample();
        example.createCriteria().andIdEqualTo(id);
        returnReasonMapper.updateByExample(record, example);
        return record;
    }

    @Cacheable(cacheNames = "OrderReturnReason", key = "#id", unless = "#result==null")
    @Override
    public OmsOrderReturnReason getOmsOrderReturnReason(Long id) {
        return returnReasonMapper.selectByPrimaryKey(id);
    }
}
